from wtforms import StringField,IntegerField
from wtforms.validators import Email,InputRequired,Length,EqualTo
from ..forms import BaseForm
from utils import qfcache
from wtforms import ValidationError
from flask import g
class LoginForm(BaseForm):
    email = StringField(validators=[Email(message="请输入正确的邮箱格式"),InputRequired(message="请输入邮箱")])
    password = StringField(validators=[Length(6,30,message="密码最短6位,最长30为")])
    remember = IntegerField()

class ResetpwdForm(BaseForm):
    oldpwd = StringField(validators=[Length(6, 30, message="请输入正确格式的旧密码")])
    newpwd = StringField(validators=[Length(6,30,message="请输入正确格式的新密码")])
    newpwd2 = StringField(validators=[EqualTo("newpwd",message="两次密码必须保持一致")])


class ResetEmailForm(BaseForm):
    email = StringField(validators=[Email(message="请输入正确的邮箱格式"), InputRequired(message="请输入邮箱")])
    captcha = StringField(validators=[Length(min=6,max=6,message="请输入正确长度的验证码")])

    def validate_captcha(self,field):
        capcha = field.data
        email = self.email.data
        captcha_cache = qfcache.get(email)
        if not captcha_cache or capcha.lower() != captcha_cache.lower():
            raise ValidationError('邮箱验证码错误')

    def validate_email(self,field):
        email = field.data
        user = g.cms_user
        if user.email == email:
            raise ValidationError('不能修改为已经存在的邮箱')

